﻿using System;
using Microsoft.Phone.BackgroundAudio;

namespace WindowsPhoneAudioStreaming.Audio.Streaming 
{
    public class PlaylistTrack
    {
        public string Source { set; get; }

        public string Title { set; get; }

        public string Artist { set; get; }

        public string Album { set; get; }

        public string AlbumArtUrl { set; get; }

        public bool IsStreaming { set; get; }

        public Uri AlbumArt
        {
            get
            {
                if (string.IsNullOrEmpty(this.AlbumArtUrl))
                {
                    return null;
                }
                else
                {
                    if (this.AlbumArtUrl.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                    {
                        return new Uri(this.AlbumArtUrl);
                    }
                    else
                    {
                        return new Uri(this.AlbumArtUrl, UriKind.Relative);

                    }
                }
            }
        }

        public EnabledPlayerControls PlayerControls { set; get; }

        public PlaylistTrack()
        {
            this.Source = "";
            this.Title = "";
            this.Artist = "";
            this.Album = "";
            this.AlbumArtUrl = "";
            this.IsStreaming = true;
            this.PlayerControls = new EnabledPlayerControls();
        }

        // Convert from this class to the standard WP 7.1 AudioTrack
        public AudioTrack ToAudioTrack()
        {
            if (this.IsStreaming)
            {
                return new AudioTrack(null, this.Title, this.Artist, this.Album, this.AlbumArt, this.Source, this.PlayerControls);
            }
            else
            {
                return new AudioTrack(new Uri(this.Source), this.Title, this.Artist, this.Album, this.AlbumArt, null, this.PlayerControls);
            }
        }
    }
}
